// reduce方法的练习
// 练习1
// '手机电脑键盘的总价为700元'

// computed的用法
import { computed } from 'vue'

const arr = [1, 2, 3, 4, 5]
// 使用reduce方法计算数组之和
function getNum (arr) {
  const arrSum = arr.reduce((sum, value) => { sum += value }, 0)
  console.log(arrSum)
  return arrSum
}
getNum(arr) // 15

// 练习2
const arr1 = [
  {
    price: 100,
    count: 10,
    name: '手机'
  },
  {
    price: 200,
    count: 20,
    name: '电脑'
  },
  {
    price: 400,
    count: 40,
    name: '键盘'
  }
]
// 定义一个函数
function getAllPrice (arr) {
  const priceRes = arr.reduce((priceSum, obj) => { priceSum += obj.price }, 0)
  const countRes = arr.reduce((priceSum, obj) => { priceSum += obj.count }, 0)
  console.log(`手机电脑键盘的总价为${priceRes}元`)
  console.log(`手机电脑键盘的总数量为${countRes}个`)
  return {
    priceRes,
    countRes
  }
}

getAllPrice(arr1)
// computed的用法
const filterArr = computed(() => {
  return arr.filter(item => item > 2)
})

// provide('key', arr1)
// const data = inject('key')
return {
  filterArr

}
